package org.car.world.controller.shop;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.car.world.common.util.Page;
import org.car.world.core.entily.CommentsTable;
import org.car.world.core.entily.MerchantsServeTable;
import org.car.world.core.entily.MerchantsTable;
import org.car.world.core.entily.OrderTable;
import org.car.world.core.entily.UserTable;
import org.car.world.core.entily.VouchersTable;
import org.car.world.service.MerchantsServeService;
import org.car.world.service.MerchantsService;
import org.car.world.service.OrderService;
import org.car.world.service.TypeService;
import org.car.world.service.UserService;
import org.car.world.service.VouchersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

/**
 * @author Administrator
 * @ClassName: OrderController
 * @Description: 订单控制器
 * @date Dec 23, 2018
 */
@Controller
@RequestMapping("shop")
public class OrderController {

    @Autowired
    OrderService orderservice;

    @Autowired
    TypeService typeservice;

    @Autowired
    UserService userservice;

    @Autowired
    MerchantsService merchantsService;

    @Autowired
    VouchersService vouchersservice;

    @Autowired
    MerchantsServeService merchantsserveservice;

    /**
     * @Title: doorder_init @Description: 访问订单列表 @param @return 参数 @return
     * String 返回类型 @throws
     */
    @RequestMapping("orderlist")
    @ResponseBody
    public Map<String, Object> doorder_init(
            @RequestParam(value = "form", required = false, defaultValue = "1") Integer form,
            @RequestParam(value = "AlltoNow", required = true) Integer AlltoNow,
            @RequestParam(value = "cj", required = false) Integer cj, HttpSession session) {
        Map<String, Object> hasmap = new HashMap<String, Object>();
        List<OrderTable> order = new ArrayList<OrderTable>();
        List<MerchantsTable> datamerlist = (List<MerchantsTable>) session.getAttribute("Merchants");
        //List<MerchantsTable> datamerlist = merchantsService.selectphone("17630389495");
        String activ = "";
        Integer count = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = new java.util.Date();
        String str = sdf.format(date);

        Page<OrderTable> Page = new Page<OrderTable>();
        Page.setCurrentPage(form);
        Page.setPageSize(7);
        for (MerchantsTable me : datamerlist) {
            //查询今日订单
            if (AlltoNow == 0) {
                count = orderservice.selectByPrimaryKeyDateListCount(me.getMerchantsId(), str);
                order = orderservice.selectByPrimaryKeyDateListLimit(me.getMerchantsId(), str, Page.getCurrentPage(),
                        Page.getPageSize());
                //查看所有该商家已完成的订单
            } else if (AlltoNow == 1 && cj == 1) {
                count = orderservice.selectByPrimaryListStaCount(me.getMerchantsId());
                order = orderservice.selectByPrimaryKeyListStaLimit(me.getMerchantsId(), Page.getCurrentPage(),
                        Page.getPageSize());
                //查询所有订单
            } else if (AlltoNow == 1) {
                count = orderservice.selectByPrimaryKeyListCount(me.getMerchantsId());
                order = orderservice.selectByPrimaryKeyListLimit(me.getMerchantsId(), Page.getCurrentPage(),
                        Page.getPageSize());

            }

            activ = me.getMerchantsActiv();
        }

        Page.setTotalRow(count);
        Page.setPageList(order);

        if (Page != null) {
            session.setAttribute("MerAcv", activ);
            session.setAttribute("OrderType", typeservice.selectTname("订单类型"));

            hasmap.put("Page", Page);
            hasmap.put("MerSer", merchantsserveservice.selectAll());
            hasmap.put("OrderStatus", typeservice.selectTname("订单状态"));
            hasmap.put("Vouchers", vouchersservice.selectAll());
            hasmap.put("VoucherType", typeservice.selectTname("红包类型"));
            hasmap.put("MerAcv", activ);
            hasmap.put("OrderType", typeservice.selectTname("订单类型"));

        }
        return hasmap;
    }

    /**
     * @Title: orderinfo @Description: 订单详情页面 @param @param
     * orderid @param @return 参数 @return String 返回类型 @throws
     */
    @RequestMapping("orderinfo")
    public String orderinfo(Integer orderid, Model mo, HttpSession session) {

        OrderTable order = orderservice.selectByPrimaryKey(orderid);
        if (StringUtils.isEmpty(order)) {
            return "shop/order";
        } else {
            UserTable user = userservice.selectByPrimaryKey(order.getUserId());
            if (user != null) {
                mo.addAttribute("User", user);
            }
            mo.addAttribute("OrderType", session.getAttribute("OrderType"));
            mo.addAttribute("MerAcv", session.getAttribute("MerAcv"));
            mo.addAttribute("MerSer", merchantsserveservice.selectAll());
            mo.addAttribute("Vouchers", vouchersservice.selectByPrimaryKey(order.getOrderVouchers()));
            mo.addAttribute("VoucherType", typeservice.selectTname("红包类型"));
            mo.addAttribute("Order", order);
            return "shop/orderinfo";
        }

    }


    /**
     * @param @param  session
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: getshopsr
     * @Description: 访问我的店铺收入
     */
    @PostMapping("mysrlist")
    @ResponseBody
    public Map<String, Object> getshopsr(@RequestParam(value = "form", required = false, defaultValue = "1") Integer form,
                                         HttpSession session, Model mo) {
        Map<String, Object> hasmap = new HashMap<String, Object>();
        List<MerchantsTable> datamerlist = new ArrayList<MerchantsTable>();
        List<OrderTable> dataordlist = new ArrayList<OrderTable>();
        List<Object> monlist = new ArrayList<Object>();
        List<VouchersTable> voulist = new ArrayList<VouchersTable>();
        List<VouchersTable> voulist1 = new ArrayList<VouchersTable>();
        Page<OrderTable> Page = new Page<OrderTable>();
        Page.setCurrentPage(form);
        Page.setPageSize(2);
        double summon = 0.0;
        double money = 0.0;
        double money1 = 0.0;
        int count = 0;
        datamerlist = (List<MerchantsTable>) session.getAttribute("Merchants");
        /// datamerlist = merchantsService.selectphone("17630389495");

        for (MerchantsTable mer : datamerlist) {
            dataordlist = orderservice.selectByPrimaryKeyListNewLimit(mer.getMerchantsId(), Page.getCurrentPage(), Page.getPageSize());
            count = orderservice.selectByPrimaryKeyListStaCount(mer.getMerchantsId());

            //获取所有该商家已完成的订单
            List<OrderTable> ordlist = orderservice.selectByPrimaryKeyListSta(mer.getMerchantsId());


            //判断优惠后的订单价格，总收入
            for (OrderTable ord : ordlist) {
                //判断是否使用代金卷
                if (ord.getOrderVouchers() == null) {
                    money = ord.getOrderPrice();
                    //使用了代金卷
                } else {
                    voulist = vouchersservice.selectByVouchersIdList(ord.getOrderVouchers(), mer.getMerchantsId());
                    for (VouchersTable vou : voulist) {
                        //使用了代金卷
                        if (ord.getOrderVouchers() == vou.getVouchersId()) {
                            //代金卷状态
                            if (vou.getVouchersState() == 1) {
                                //判断订单是否满足红包条件
                                if (ord.getOrderPrice() >= vou.getVouchersFull()) {
                                    money = ord.getOrderPrice() - vou.getVouchersPrice();
                                }
                            } else if (vou.getVouchersState() == 2) {
                                money = ord.getOrderPrice();
                            }
                        }
                    }
                }
                summon += money;

            }

            //判断每个订单是否优惠的价格
            for (OrderTable ords : dataordlist) {

                //判断是否使用代金卷
                if (ords.getOrderVouchers() == null) {
                    money1 = ords.getOrderPrice();
                    //使用了代金卷
                } else {
                    voulist1 = vouchersservice.selectByVouchersIdList(ords.getOrderVouchers(), mer.getMerchantsId());
                    for (VouchersTable vou : voulist1) {
                        System.err.println(ords.getOrderVouchers() + "-" + vou.getVouchersId());
                        //使用了代金卷
                        if (ords.getOrderVouchers() == vou.getVouchersId()) {

                            //代金卷状态
                            if (vou.getVouchersState() == 1) {
                                //判断订单是否满足红包条件
                                if (ords.getOrderPrice() >= vou.getVouchersFull()) {
                                    money1 = ords.getOrderPrice() - vou.getVouchersPrice();
                                }
                            } else if (vou.getVouchersState() == 2) {
                                money1 = ords.getOrderPrice();
                            }
                        }
                    }
                }
                monlist.add(money1);
            }
        }
        Page.setTotalRow(count);
        Page.setPageList(dataordlist);
        hasmap.put("Page", Page);
        hasmap.put("moneylist", monlist);
        hasmap.put("summon", summon);
        return hasmap;
    }

}
